package controller

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"myproject/model"

	"myproject/schemas"
)

// 总的路由分发
func New(userGrp *gin.RouterGroup) {
	userGrp.Use().GET("/getNews", getNews)
	userGrp.Use().POST("/addnew", addNews)
}

// 获取新闻的具体接口  采用Qusery
func getNews(c *gin.Context) {
	var news []model.News
	db := model.GetDb()
	db.Table("news").Find(&news)
	for _, i := range news {
		fmt.Println(i)
	}
	c.JSON(200, gin.H{
		"code":    "10010",
		"neslist": news,
	})
}

// 添加新闻的接口 采用PostForm获取参数  返回的是一个字符串
func addNews(c *gin.Context) {
	news := &schemas.News{}
	//绑定参数
	_ = c.Bind(news)
	//&取地址的意思
	news1 := &model.News{
		//Id:      news.Id,
		Title:   news.Title,
		Content: news.Content,
		Userid:  news.Userid,
	}
	db := model.GetDb()
	res := db.Create(news1)
	c.JSON(200, gin.H{
		"code": "10010",
		"res":  res,
		"msg":  "添加新闻车成功",
	})
}

//json的接收方式  Get
